package org.jprobe.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
/**
*
* Menu bar controller
*
* @author Hussain Al-Mutawa
* @version 1.0
* @since 1.0
*/

public class MenuHandler extends JMenuBar implements ActionListener{
	/** serial version uid 	**/
	private static final long serialVersionUID    = -6457544231059544591L;
	/** file menu actions 	**/
	private final static transient String[]FILE   = new String[]{"Exit"};
	/** tools menu actions 	**/
	private final static transient String[]TOOLS  = new String[]{"Settings"};
	/** help menu actions 	**/
	private final static transient String[]HELP   = new String[]{"About"};
	/** main menu items 	**/
	private final static transient String[][]MAIN = new String[][]{FILE,TOOLS,HELP};
	/** main menu titles 	**/
	private final static transient String[]TITLES = new String[]{"File","Tools","Help"};

	/** initializes the menu controller **/
	public MenuHandler() {
		super();
		for(int index=0;index<MAIN.length;index++){
			addMenu(index);
		}
	}
	/**
	 * adds a menu
	 * @param index-the index of the menu in the array
	 **/
	private void addMenu(final int index) {
		final JMenu menu = new JMenu(TITLES[index]);
		for(final String mi:MAIN[index]){
			menu.add(getSubMenu(mi));
		}
		this.add(menu);
	}
	/**
	 * gets a sub menu from the array of submenus
	 * @param menuItem
	 * @return
	 */
	private JMenuItem getSubMenu(final String menuItem) {
		final JMenuItem jmi = new JMenuItem(menuItem);
		jmi.addActionListener(this);
		return jmi;
	}
	@Override
	public void actionPerformed(final ActionEvent actionEvent) {
		final JMenuItem jmi = (JMenuItem)actionEvent.getSource();
		if(jmi.getText().equals("Exit")){
			Runtime.getRuntime().exit(0);
		}else if(jmi.getText().equals("About")){
			AboutWindow.open();
		}
	}
}
